package configs

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"os"

	// "path"
	"path/filepath"
	// "runtime"
	"strings"
)

type Environment struct {
	Port            string `json:"port"`
	
	MysqlDns  string `json:"mysql_dsn"`

}
type Config map[string]Environment
type AppConfig struct {
	Environment string `json:"environment"`
	Config      Config `json:"config"`
}

func GetCurrentDirectory() string {
	
	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		log.Fatal(err)
	}

	//将\替换成/
	return strings.Replace(dir, "\\", "/", -1)
}
func LoadConfig() Environment {

	configPath := "."
	content, err := ioutil.ReadFile(configPath + "/config.json")
	if err != nil {
		panic(err)
	}
	var config AppConfig
	json.Unmarshal(content, &config)
	CurrentEnvironment = config.Environment
	return config.Config[config.Environment]
}

var App Environment
var CurrentEnvironment string

func init() {
	App = LoadConfig()
}
